package com.mbc.rmt.manager;

import com.mbc.common.dao.CommonDAOFactory;
import com.mbc.common.dao.CurrencyDAO;
import com.mbc.common.dao.DBManager;
import com.mbc.common.dao.DBManagerFactory;
import com.mbc.common.dao.LogDAO;
import com.mbc.common.entities.Currency;
import com.mbc.common.entities.Log;
import com.mbc.common.manager.AbstractMgr;
import com.mbc.common.param.CriteriaParams;
import com.mbc.common.utils.KeyGenerator;
import com.mbc.rmt.dao.COACurrencyDAO;
import com.mbc.rmt.dao.COADAO;
import com.mbc.rmt.dao.DepartmentDAO;
import com.mbc.rmt.dao.General_COADAO;
import com.mbc.rmt.dao.IAIMSCOACurrencyDAO;
import com.mbc.rmt.dao.IAIMSCOADAO;
import com.mbc.rmt.dao.IAIMSDepartmentDAO;
import com.mbc.rmt.dao.RemittanceDAOFactory;
import com.mbc.rmt.data.General_COADataList;
import com.mbc.rmt.entities.COA;
import com.mbc.rmt.entities.COACurrency;
import com.mbc.rmt.entities.Department;
import com.mbc.rmt.entities.General_COA;

public class General_COAMgr extends AbstractMgr {

	private General_COADAO generalCOADAO;
	private COADAO coaDAO;
	private DepartmentDAO departmentDAO;
	private COACurrencyDAO l_DAOCOACurrency;
	private CurrencyDAO currencyDAO;
	
	private IAIMSCOADAO iaimscoaDAO;
	private IAIMSDepartmentDAO iaimsdepartmentDAO;
	private IAIMSCOACurrencyDAO iaimscoacurrencyDAO;
	private LogDAO logDAO;

	public General_COAMgr() {

	}

	public General_COAMgr(String sessionId) {
		this.sessionId = sessionId;
	}

	public void create(General_COA entity) throws Exception {
		DBManager l_DBManger = DBManagerFactory.getDBManager(sessionId, DBManager.LINKED_IAIMS);
		
		openConnection();
		try {
			beginTransaction();
			l_DBManger.beginTransaction();
			
			generalCOADAO = RemittanceDAOFactory.getGeneral_COADAO(getConnection());
			departmentDAO = RemittanceDAOFactory.getDepartmentDAO(getConnection());
			l_DAOCOACurrency = RemittanceDAOFactory.getCOACurrencyDAO(getConnection());
			coaDAO = RemittanceDAOFactory.getCOADAO(getConnection());
			currencyDAO = CommonDAOFactory.getCurrencyDAO(getConnection());
			logDAO = CommonDAOFactory.getLogDAO(getConnection());
			
			iaimscoaDAO = RemittanceDAOFactory.getIAIMSCOADAO(l_DBManger.getConnection());
			iaimscoacurrencyDAO = RemittanceDAOFactory.getIAIMSCOACurrencyDAO(l_DBManger.getConnection());
			iaimsdepartmentDAO = RemittanceDAOFactory.getIAIMSDepartmentDAO(l_DBManger.getConnection());
	
//			entity.setBranch_id(KeyGenerator.generateKey(getConnection()));
//			generalCOADAO.create(entity);

			COA l_entityCOA = new COA();
			Department l_entityDepartment = new Department();
			COACurrency l_entityCOACurrency = new COACurrency();
			
			Currency l_curreny = currencyDAO.findCurrencyByCode("' or 1 = 1 --");

			long departmentkey = 0;
			long coakey = 0;

			l_entityCOA.setAC_ID("_");
			l_entityCOA.setBranchId(entity.getBranch_id());
			l_entityCOA = coaDAO.getInfo(l_entityCOA);

			if (l_entityCOA == null) {
				l_entityDepartment.setDepartment_code("_");
				l_entityDepartment.setBranch_id(entity.getBranch_id());
				l_entityDepartment = departmentDAO.getInfo(l_entityDepartment);

				if (l_entityDepartment == null) {
					l_entityDepartment = new Department();
					l_entityDepartment.setDepartment_id(KeyGenerator.generateKey(getConnection()));
					departmentkey = l_entityDepartment.getDepartment_id();
					l_entityDepartment.setDepartment_code("_");
					l_entityDepartment.setBranch_id(entity.getBranch_id());
					l_entityDepartment.setDescription("Blank Department");

					departmentDAO.create(l_entityDepartment);
					iaimsdepartmentDAO.create(l_entityDepartment);
				} else
					departmentkey = l_entityDepartment.getDepartment_id();
				
				l_entityCOA = new COA();

				l_entityCOA.setCOAKey(KeyGenerator.generateKey(getConnection()));
				coakey = l_entityCOA.getCOAKey();
				l_entityCOA.setAC_ID("_");
				l_entityCOA.setDepartmentKey(departmentkey);
				l_entityCOA.setDescription("Blank COA");
				l_entityCOA.setACType(1);
				l_entityCOA.setACGroup(1);
				l_entityCOA.setPost_In("A");
				l_entityCOA.setACStatus("A");
				l_entityCOA.setCreatedDate("");
				l_entityCOA.setUpdatedDate("");
				l_entityCOA.setCreatedUser(1);
				l_entityCOA.setUpdatedUser(1);
				l_entityCOA.setCashFlow("_");
				l_entityCOA.setBranchId(entity.getBranch_id());

				l_entityCOA.setDefault_Currency(l_curreny.getCurrencyId());
				coaDAO.create(l_entityCOA);
				iaimscoaDAO.create(l_entityCOA);

				l_entityCOACurrency.setCoakey(coakey);
				l_entityCOACurrency.setDepartmentkey(departmentkey);
				l_entityCOACurrency.setCoacurrencykey(KeyGenerator.generateKey(getConnection()));
				l_entityCOACurrency.setCurrencykey(l_curreny.getCurrencyId());
				l_DAOCOACurrency.create(l_entityCOACurrency);
				iaimscoacurrencyDAO.create(l_entityCOACurrency);
			} else {
				coakey = l_entityCOA.getCOAKey();
				departmentkey = l_entityCOA.getDepartmentKey();
			}

			if (entity.getLiabilitiesAccountID() == 0)
				entity.setLiabilitiesAccountID(coakey);
			if (entity.getLiabilitiesDepartmentID() == 0)
				entity.setLiabilitiesDepartmentID(departmentkey);

			if (entity.getCashAccountID() == 0)
				entity.setCashAccountID(coakey);
			if (entity.getCashDepartmentID() == 0)
				entity.setCashDepartmentID(departmentkey);

			if (entity.getExchangeRoundingErrorAccountID() == 0)
				entity.setExchangeRoundingErrorAccountID(coakey);
			if (entity.getExchangeRoundingErrorDepartmentID() == 0)
				entity.setExchangeRoundingErrorDepartmentID(departmentkey);

			if (entity.getExtraFeesAccountID() == 0)
				entity.setExtraFeesAccountID(coakey);
			if (entity.getExtraFessDepartmentID() == 0)
				entity.setExtraFessDepartmentID(departmentkey);

			if (entity.getCR_GeneralAccountSetID() == 0) {
				entity.setCR_GeneralAccountSetID(KeyGenerator.generateKey(dbManager.getConnection()));
				generalCOADAO.create(entity);
				
				Log l_logentity = new Log();
				l_logentity.setTablename("General_COA");
				l_logentity.setLogkey(KeyGenerator.generateKey(getConnection()));
				l_logentity.setKey(entity.getCR_GeneralAccountSetID());
				l_logentity.setUserid(entity.getUserid());
				l_logentity.setLogstatus(Log.__SAVE_DATA);
				logDAO.getData(l_logentity);
			} else {
				entity.setCR_GeneralAccountSetID(entity.getCR_GeneralAccountSetID());
				generalCOADAO.update(entity);
				
				Log l_logentity = new Log();
				l_logentity.setTablename("General_COA");
				l_logentity.setLogkey(KeyGenerator.generateKey(getConnection()));
				l_logentity.setKey(entity.getCR_GeneralAccountSetID());
				l_logentity.setUserid(entity.getUserid());
				l_logentity.setLogstatus(Log.__UPDATE_DATA);
				logDAO.getData(l_logentity);
			}
			
			l_DBManger.commitTransaction();
			commintTransaction();
		} catch (Exception e) {
			l_DBManger.rollBackTransaction();
			rollbackTransaction();
			throw e;
		} finally {
			l_DBManger.closeConnection();
			closeConnection();
		}
	}

	public void delete(long key, long userid) throws Exception {
		openConnection();
		try {
			generalCOADAO = RemittanceDAOFactory.getGeneral_COADAO(dbManager
					.getConnection());
			logDAO = CommonDAOFactory.getLogDAO(getConnection());
			
			Log l_logentity = new Log();
			l_logentity.setTablename("General_COA");
			l_logentity.setLogkey(KeyGenerator.generateKey(getConnection()));
			l_logentity.setKey(key);
			l_logentity.setUserid(userid);
			l_logentity.setLogstatus(Log.__DELETE_DATA);
			logDAO.getData(l_logentity);
			
			generalCOADAO.delete(key);
		} catch (Exception e) {
			throw e;
		} finally {
			closeConnection();
		}
	}

	public General_COA find(long key) throws Exception {
		openConnection();
		try {
			generalCOADAO = RemittanceDAOFactory.getGeneral_COADAO(dbManager
					.getConnection());
			return generalCOADAO.find(key);
		} catch (Exception e) {
			throw e;
		} finally {
			closeConnection();
		}
	}

	public General_COADataList findGeneral_COA(CriteriaParams cri)
			throws Exception {
		openConnection();
		try {
			generalCOADAO = RemittanceDAOFactory.getGeneral_COADAO(dbManager
					.getConnection());
			return generalCOADAO.findGeneral_COAs(cri);
		} catch (Exception e) {
			throw e;
		} finally {
			closeConnection();
		}
	}

}
